# -*- coding: utf-8 -*-
# JS命令

class JsScript():
    """常用脚本。由于driver不保存运行结果，使用时+=连起来使用"""

    # xpath寻找元素
    findElementByXpath = """
function getElementsByXpath(xpathToExecute, element) {
    element = (element === undefined)? document:element;
    var result = [];
    var nodesSnapshot = document.evaluate(xpathToExecute, element, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    for (var i = 0; i < nodesSnapshot.snapshotLength; i++) {
        result.push(nodesSnapshot.snapshotItem(i));
    }
    return result;
};
"""

    # 发送POST请求跳转到指定页面
    webHttpPost = """
function httpPost(URL, PARAMS) {
    var temp = document.createElement("form");
    temp.action = URL;
    temp.method = "post";
    temp.style.display = "none";
    
    for (var x in PARAMS) {
        var opt = document.createElement("textarea");
        opt.name = x;
        opt.value = PARAMS[x];
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
};
"""
